﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using GtsServer.Manage.Controller;
using GtsServer.Common;
using GtsServer.Model;

namespace GtsServer.Manage.IISPage.Ajax
{
    /// <summary>
    /// TaskTypeAjax 的摘要说明
    /// </summary>
    public class TaskTypeAjax : IHttpHandler
    {
        TaskTypeController controller = new TaskTypeController();
        public void ProcessRequest(HttpContext context)
        {
            string type = string.Empty;
            if (context.Request["action"] != null)
            {
                type = context.Request["action"];

                switch (type)
                {
                    case "GetList":
                        GetList(context);
                        break;
                    case "Add":
                        Add(context);
                        break;
                    case "Update":
                        Update(context);
                        break;
                    case "Delete":
                        Delete(context);
                        break;
                    default:
                        break;
                }
            }
        }

        private void Delete(HttpContext context)
        {
            int id = Convert.ToInt32(context.Request["id"].ToString());
            context.Response.Write(controller.Delete(id));
        }

        private void Update(HttpContext context)
        {
            int id = Convert.ToInt32(context.Request["id"].ToString());
            string name = context.Request["name"];
            TaskType model = new TaskType()
            {
                ID = id,
                Name = name
            };
            context.Response.Write(controller.Update(model));
        }

        private void Add(HttpContext context)
        {
            int id = Convert.ToInt32(context.Request["id"].ToString());
            string name = context.Request["name"];
            TaskType model = new TaskType()
            {
                ID = id,
                Name = name
            };
            context.Response.Write(controller.Add(model));
        }

        private void GetList(HttpContext context)
        {
            context.Response.Write(JsonHelper.ListToJson<TaskType>(controller.GetList()));
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}